package com.js.sys.fragment.view.first;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import com.js.lib_common.base.BasePresenter;
import com.js.lib_common.base.MvpFragment;
import com.js.sys.R;
import com.js.sys.adapter.BindingHistoryAdapter;
import com.js.sys.greendao.DBHelper;
import com.js.sys.greendao.dao.BindingHistoryDao;
import com.js.sys.greendao.db.BindingHistory;
import com.miguelcatalan.materialsearchview.MaterialSearchView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 记录
 * Created by wz on 2021/5/19.
 */
public class BindHistoryFragment extends MvpFragment {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.rcy_list)
    RecyclerView rcyList;
    private static final String ARG_BEAN = "type";

    @BindView(R.id.search_view)
    MaterialSearchView searchView;
    List<BindingHistory> list;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private int type;

    public static BindHistoryFragment newInstance(int type) {
        Bundle args = new Bundle();
        BindHistoryFragment fragment = new BindHistoryFragment();
        args.putInt(ARG_BEAN, type);
        fragment.setArguments(args);
        return fragment;

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_first_bind_history;
    }

    @Override
    protected void initView() {
        list = new ArrayList<>();
        initToolbarNav(toolbar);
        toolbarTitle.setText("录入记录");
        Bundle bundle = getArguments();
        BindingHistoryAdapter mAdapter = new BindingHistoryAdapter(_mActivity);
        initRecyclerView(rcyList, mAdapter, 1);
        initSmartRefreshLayout(refreshLayout);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mAdapter.getData().clear();
                list = DBHelper.getInstance().getSession().getBindingHistoryDao()
                        .queryBuilder().where(BindingHistoryDao.Properties.Type.eq(type))
                        .orderDesc(BindingHistoryDao.Properties.Id).list();
                mAdapter.setNewData(list);
                mAdapter.notifyDataSetChanged();
                refreshLayout.finishRefresh(1000);
            }
        });
        toolbar.inflateMenu(R.menu.menu_search);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.action_search) {
                    searchView.setMenuItem(item);
                }
                return false;
            }
        });

        if (bundle != null) {
            type = bundle.getInt(ARG_BEAN);
            list = DBHelper.getInstance().getSession().getBindingHistoryDao()
                    .queryBuilder().where(BindingHistoryDao.Properties.Type.eq(type))
                    .orderDesc(BindingHistoryDao.Properties.Id).list();

        }

        searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
//                mAdapter.getData().clear();
                list = DBHelper.getInstance().getSession().getBindingHistoryDao()
                        .queryBuilder()
                        .where(BindingHistoryDao.Properties.Json.like("%" +query+"%"),
                                BindingHistoryDao.Properties.Type.eq(type))
                        .orderDesc(BindingHistoryDao.Properties.Id)
                        .list();
                mAdapter.setNewData(list);

                toastShow(list.size() + "-");
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        for (BindingHistory bindingHistory : list) {
            Log.e("11111",bindingHistory.getJson());
        }
        mAdapter.setNewData(list);


    }

}
